import 'package:flutter/material.dart';
import 'package:mall/core/providers/pages/mine/manage/bank_type_model.dart';
import 'package:mall/core/providers/view_model.dart';
import 'package:provider/provider.dart';

class BankTypePage extends StatelessWidget {
  static const routeName = '/BankTypePage';
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => BankTypeModel(Provider.of(context, listen: false)),
      child: Scaffold(
        appBar: AppBar(
          title: Text('账户类型'),
        ),
        body: Consumer<BankTypeModel>(
          builder: (context, model, child) {
            switch (model.viewState) {
              case ViewState.busy:
                return Center(
                  child: CircularProgressIndicator(),
                );
              case ViewState.error:
                return Center(
                  child: Text(model.errorMessage),
                );
              case ViewState.success:
                return _ListWidget(
                  list: model.map.entries.toList(),
                );
              default:
                return Container();
            }
          },
        ),
      ),
    );
  }
}

class _ListWidget extends StatelessWidget {
  final List<MapEntry<String, dynamic>> list;

  const _ListWidget({
    Key key,
    @required this.list,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView.separated(
      itemBuilder: (context, index) {
        var mapEntry = list[index];
        return Material(
          color: Colors.white,
          child: ListTile(
            title: Text(mapEntry.value),
            trailing: Icon(Icons.keyboard_arrow_right),
            onTap: () => _onItemClicked(context, mapEntry),
          ),
        );
      },
      separatorBuilder: (context, index) {
        return Divider(height: 1);
      },
      itemCount: list.length,
    );
  }

  void _onItemClicked(BuildContext context, MapEntry<String, dynamic> mapEntry) {
    Navigator.of(context).pop(mapEntry);
  }
}
